<ol class="breadcrumb">
    <li><a href="#">一元中奖</a></li>
    <li>商品管理</li>
</ol>
<div class="panel panel-default">
    <div class="panel-heading">
        <span class="icon-gift icon-large"></span>
        <span class="panel-title">商品列表</span>
    </div>
    <div class="panel-body">
        <% if(session.permissionMap[3000]){ %>
        <a href="/goods/add" class="btn btn-success btn-sm">新增</a>
        <% } %>
        <% if(session.permissionMap[3002]){ %>
        <a href="#" onclick="getCheckIds()" class="btn btn-primary btn-sm">编辑</a>
        <% } %>
        <% if(session.permissionMap[3001]){ %>
        <a href="#" onclick="del()" class="btn btn-danger btn-sm">删除</a>
        <% } %>
    </div>
    <div class="table-responsive">
        <table class="table table-striped" id="goodsTable">
            <thead>
            <tr>
                <th>√</th>
                <th>#</th>
                <th>商品名称</th>
                <th>商品描述</th>
                <th>商品编号</th>
                <th>价格</th>
                <th>参与积分</th>
                <th>状态</th>
                <th>创建时间</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <td colspan="11">
                    <nav class="twbsPagination"></nav>
                </td>
            </tr>
            </tfoot>
            <tbody>
            <% data.rows.forEach(function(goods, index){ %>
            <tr>
                <th><input name="goods_id" type="checkbox" value="<%= goods.id %>"></th>
                <th scope="row"><%= index + 1 %></th>
                <td><%= goods.title %></td>
                <td><%= goods.description %></td>
                <td><%= goods.goodssn %></td>
                <td><%= goods.productprice %></td>
                <td><%= goods.credit %></td>
                <td><%= goods.status %></td>
                <td><%= goods.createtime %></td>

            </tr>
            <% }) %>

            </tbody>
        </table>
    </div>
</div>
<script>
    /*
     $('#openModal').click(function(){
     $('#myModal').modal('toggle');
     });*/
    init(Number(<%= data.total%>));//初始化分页
    function init(total) {
        //分页功能
        $('.twbsPagination').twbsPagination({
            totalPages: total % 10 == 0 ? total / 10 : (total / 10) + 1,
            visiblePages: 10,
            onPageClick: function (event, page) {
                reloadpage(page, false);
            }
        });
    }
    function reloadpage(page, isinit) {
        $.get("/goods/list",
                {page: page},
                function (result) {
                    if (isinit) {
                        init(result.total);
                    }

                    if (result.rows.length < 1) {
                        $('#rowcontent').html('无数据...');
                        return;
                    }
                    var html = '';
                    result.rows.forEach(function (goods, index) {
                        html += '<tr>' +
                                '<td><input name="goods_id" type="checkbox" value="' + goods.id + '"></td> ' +
                                '<td scope="row">' + (index + 1) + '</td>' +
                                '<td>' + goods.title + '</td>' +
                                '<td>' + goods.description + '</td> ' +
                                '<td>' + goods.goodssn + '</td> <td>'
                                + goods.productprice + '</td> ' +
                                '<td>' + goods.credit + '</td> ' +
                                '<td>' + goods.status + '</td> ' +
                                '<td>' + goods.createtime + '</td>' +
                                '</tr>';
                    });
                    $('#goodsTable tbody').html(html);
                });
    }


    function getCheckIds() {
        var adIds = "";
        $("input:checkbox[name='goods_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            }
        });
        if (!adIds) {
            return alert('请选择一件商品!');
        }
        window.location.href = "/goods/edit?id=" + adIds;
    }
    function getCheckIds1() {
        var adIds = "";
        $("input:checkbox[name='goods_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
//            alert(adIds);
        });

    }
    function del() {
        var id = selectid();
        if (!id) {
            return alert('请选择一条记录!');
        }
        $.post('/goods/del', {id: id}, function (result) {
            if (result.msg) {
                location.reload(true);
            } else {
                alert('失败！');
            }
        });
    }

    function selectid() {
        var adIds = "";
        $("input:checkbox[name='goods_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();

            }
        });
        if (!adIds) {
            return alert('请选择一条记录!');
        }
        return adIds;
    }
</script>
</html>